//
// Created by wsl on 1/6/23.
//
#include "defect_depth.h"
#include "json_parser.h"
#include <iostream>
#include "circle_detect.h"

//int main()
//{
//
//    double a[3][3]={1.22460067e-02, 2.28029013e-01 ,1.41333425e+02,
//    2.37765244e-01 ,3.11390601e-02, 5.18306496e+02,
//    8.96787800e-06 ,1.03789330e-05 ,2.42406882e-01};
////    double a[3][3]={7.13759620e-03  ,9.98131529e-01 , 4.94163994e+02,
////    -8.23321769e-01 , 3.63035573e-01  ,6.11277215e+03,
////    1.87014403e-05  ,6.72487407e-05,  1.00000000e+00};
////    //1-50
//    cv::Mat M(3,3,CV_64F,a);
//
//    auto model=DefectDepth();
//    std::string depth_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/DataSet/3d/负极所有相机图片/height/camera0/54.tif";
//    std::string mask_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/mask.png";
//    std::string json_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/DataSet/3d/负极所有相机图片/camera4/ok/camera4_0_0000_20230406_105506_318_15.json";
//    std::string gray_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/DataSet/3d/负极所有相机图片/gray/00/54.png";
//
//    std::vector<std::vector<cv::Point>> points{};
//    parse_json(json_path,points);
//    cv::Mat depth=cv::imread(depth_path,-1);
//    auto res=model.get_depth_minmax(depth,points,M);
//    for(auto pp:res)
//    {
//        std::cout<<pp<<std::endl;
//    }
//    cv::Mat gray=cv::imread(gray_path);
//    std::vector<std::vector<cv::Point>> dst{};
//    for(auto pp:points)
//    {
//        pp= warpPoly(pp,M);
//        dst.push_back(pp);
//    }
//    cv::polylines(gray,dst, true,cv::Scalar(0,0,255),2);
//    cv::imwrite("/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/12.jpg",gray);
//}


//
//    std::string depth_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/12.tif";
//    std::string mask_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/mask.png";
//    std::string json_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/camera4_0_0000_20230327_133254_808_30.json";
//
//    std::string depth_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1-50/13.tif";
//    std::string mask_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1-50/mask.png";
//    std::string json_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1-50/13.json";
//    std::vector<std::vector<cv::Point>> points{};
//    parse_json(json_path,points);
//    cv::Mat depth=cv::imread(depth_path,-1);
//    cv::Mat detect_mask=cv::imread(mask_path);
//    auto model=DefectDepth();
//    auto res=model.get_depth_basemask(depth,detect_mask,points,M);
//    for(auto pp:res)
//    {
//        std::cout<<pp<<std::endl;
//    }
//    std::cout<<"over";
//}

int main_test_circle()
{
    Circle a{cv::Point (500,500),100.0};
    cv::Point b(700,600);
    auto pp= getPointsOnLine(a,b);

    cv::Mat im=cv::Mat::zeros(cv::Size(800,800),CV_8UC3);
    cv::circle(im,a.center,int(a.radius),cv::Scalar(255,0,0),1);
    cv::circle(im,b,1,cv::Scalar(0,255,0),1);
    for(auto aa:pp)
    {
        cv::circle(im,aa,1,cv::Scalar(0,0,255),1);
    }
    cv::line(im,a.center,b,cv::Scalar(255,255,0));
    cv::imshow("a",im);
    cv::waitKey(0);
}

//int main_hough()
//{
//    cv::Mat mask=cv::imread("/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/mask.png")*255;
//    std::vector<cv::Mat> channels{};
//    cv::split(mask.clone(),channels);
//    std::vector<cv::Vec3f> circles{};
//    cv::HoughCircles(channels[0],circles,cv::HOUGH_GRADIENT,1,40,250,30,0,2000);
//
//    cv::Mat colorImage=mask.clone();
//    //cvtColor(mask, colorImage, cv::COLOR_GRAY2BGR);
//    for (size_t i = 0; i < circles.size(); i++) {
//        cv::Vec3f circle = circles[i];
//        cv::Point center(circle[0], circle[1]);
//        int radius = circle[2];
//        cv::circle(colorImage, center, radius, cv::Scalar(0, 0, 255), 2);
//    }
//    cv::imshow("a",colorImage);
//    cv::waitKey(0);
//}

//int main()
//{
//        auto model=DefectDepth();
//    std::string depth_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/Downloads/gray2_5等3个文件/height2.tif";
//    std::string mask_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/mask.png";
//    std::string json_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/Downloads/gray2_5等3个文件/gray2_5.json";
//    std::string gray_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/Downloads/gray2_5等3个文件/gray2.png";
//
//    Circle c0{cv::Point {2127,4268},400};
//    //Circle c0{cv::Point {1326,1479},450};
//    std::vector<std::vector<cv::Point>> points{};
//    parse_json(json_path,points);
//    cv::Mat depth=cv::imread(depth_path,-1);
//    cv::Mat gray=cv::imread(gray_path);
//    auto res=model.get_line_planev2_depth(depth,gray,points,c0,depth);
//    for(auto cc:res)
//    {
//        std::cout<<cc<<std::endl;
//    }
//    //model.test_line_plane_depth(depth,gray,points[1],c0);
//    cv::imwrite(gray_path+".jpg",gray);
//}

int main()
{
            auto model=DefectDepth();
    std::string depth_path="/home/wsl/下载/新建文件夹 (1)/╨┬╜¿╬─╝■╝╨/height2.tif";
    std::string gray_path="/home/wsl/下载/新建文件夹 (1)/╨┬╜¿╬─╝■╝╨/gray2.png";
//    std::string depth_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/Downloads/gray2_5等3个文件/height2.tif";
//    std::string mask_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/temp/3d/1/mask.png";
//    std::string json_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/Downloads/gray2_5等3个文件/gray2_5.json";
//    std::string gray_path="/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/Downloads/gray2_5等3个文件/gray2.png";
    cv::Mat depth=cv::imread(depth_path,-1);
    cv::Mat gray=cv::imread(gray_path);
    cv::Point c0{2160,4300+1400};
    auto aa=model.get_hangao(depth,gray,c0);
    std::cout<<aa<<std::endl;
    cv::imwrite("1.png",gray);
}

// test warp matrix
//int main()
//{
////0-40
////    double a[3][3]={-1.46564992e-04,  6.51376898e-02,  5.27314137e+01,
////    6.42035640e-02 ,-1.94646777e-04 , 1.97386358e+02,
////    -3.12314683e-07,  5.36458570e-08  ,8.51553223e-02};
////51
////    double a[3][3]={-8.91651569e-06, -4.22185646e-04  ,1.51137438e+00,
////    4.13116023e-04, -6.93940182e-06  ,2.00903152e+00,
////    -2.43815163e-09 ,-4.63238659e-10 , 5.50174790e-04};
////50
//    double a[3][3]={1.15805804e-04,  2.34975652e-03,  2.14953287e+00,
//    2.49194859e-03 ,-1.20340523e-04  ,2.77356533e+00,
//    4.37404606e-08,-4.35074442e-08 , 3.13095420e-03};
//
//    cv::Mat M(3,3,CV_64F,a);
//    std::string root="/home/wsl/下载/图片(4)/ͼƬ/8/";
//    std::string gray_path=root+"gray1.png";
//    std::string json_path=root+"5.json";
//    std::string mask_rgb_path=root+"5.png.jpg";
//    std::string mask_gray_path=gray_path+".jpg";
//    std::string height_path=root+"height1.tif";
//
//    cv::Mat depth=cv::imread(height_path,-1);
//    cv::Mat mask_gray=cv::imread(mask_gray_path);
//    cv::Mat mask_rgb=cv::imread(mask_rgb_path);
//    auto circles_rgb= get_circle(mask_rgb);
//    auto circles_gray= get_circle(mask_gray);
//    //circles_gray[0]={2149,4305};
//    circles_gray[0]={1293,1537};
//    M= modifyMatrix(circles_gray,circles_rgb,5,M);
//    cv::Mat gray=cv::imread(gray_path);
//    std::vector<std::vector<cv::Point>> points{};
//    parse_json(json_path,points);
//    std::vector<std::vector<cv::Point>> dst{};
//
//    for(auto pp:points)
//    {
//        pp= warpPoly(pp,M);
//        dst.push_back(pp);
//    }
//    auto model=DefectDepth();
//    Circle c0{circles_gray[0],375};
//    auto res=model.get_line_planev2_depth(depth,gray,points,c0,M);
////    auto res=model.get_plane_depth(depth,gray,points,c0,270,375,M);
//    for(auto pp:res)
//    {
//        std::cout<<pp<<std::endl;
//    }
//    cv::polylines(gray,dst, true,cv::Scalar(0,0,255),2);
////    cv::warpPerspective(src_im,src_im,m,dst_im.size());
////    cv::addWeighted(dst_im,0.4,src_im,0.6,0,dst_im);
//    cv::imwrite(gray_path+".bmp",gray);
//}
//
//int main()
//{
//    std::string path4="/home/wsl/下载/base/bgr/5.png";
//    std::string path0="/home/wsl/下载/base/gray/1.png";
//    std::string json_path="/home/wsl/下载/base/bgr/5.json";
//
//    auto src_im=cv::imread(path4);
//    auto dst_im=cv::imread(path0);
//
//    auto src_mask=cv::imread(path4+".jpg");
//    auto dst_mask=cv::imread(path0+".jpg");
//
//    auto m=calMatrix(dst_mask,src_mask,5,1);
////    for(int i=0;i<9;i++)
////    {
////        double *data=(double *)m.data;
////        std::cout<<data[i]<<std::endl;
////    }
//    std::vector<std::vector<cv::Point>> points{};
//    parse_json(json_path,points);
//    std::vector<std::vector<cv::Point>> dst{};
//    for(auto pp:points)
//    {
//        pp= warpPoly(pp,m);
//        dst.push_back(pp);
//    }
//    cv::polylines(dst_im,dst, true,cv::Scalar(0,0,255),2);
////    cv::warpPerspective(src_im,src_im,m,dst_im.size());
////    cv::addWeighted(dst_im,0.4,src_im,0.6,0,dst_im);
//    cv::imwrite("1.jpg",dst_im);
//}

//int main()
//{
//    auto mask=cv::imread("/media/wsl/a9f0161f-7971-c843-8c81-c68049a0235a/11/gray1.png.jpg");
//    auto a= get_circle(mask);
//    std::cout<<1;
//}